#!/usr/bin/env node

const http = require('http'),
      fs = require('fs'),
      log = console.log;

const server = http.createServer();
server.listen(8080);
server.on('request', (req, res) => {
  log(`${req.method} ${req.url} HTTP/${req.httpVersion}`);
  log(req.headers);
  log('');

  let file = __dirname + req.url;

  //错误处理方式一：主动防御
  if(fs.existsSync(file)) {
    fs.createReadStream(file).pipe(res);
  } else {
    res.statusCode = 404;
    res.end(req.url + 'not exist!');
  }
  
  /* 错误处理方式二： error 事件捕获
  let s = fs.createReadStream(file);
  s.on('error', (err) => {
    console.log(err.message);
    res.statusCode = 404;
    res.end(err.message);
  })
  s.pipe(res);
  */
});
